package com.hyh.textMessage.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.jetbrains.annotations.NotNull;

import java.util.Random;

public class NumberUtil {
    public static String send(String phone, String key){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tFHASJjq5vtSTgx7i5H", "AGxWoYiyq8beX8VehzIHzkOYxw0w0p");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = getCommonRequest(phone, key);
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }
    @NotNull
    private static CommonRequest getCommonRequest(String phone, String key) {
        CommonRequest request = new CommonRequest();//组装请求对象
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);//设置post提交
        request.setDomain("dysmsapi.aliyuncs.com");//短信API产品域名（接口地址固定，无需修改）
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "Titk购物");//短信签名
        request.putQueryParameter("TemplateCode", "SMS_465338252");
        request.putQueryParameter("TemplateParam", "{\"name\":\"" + key + "\"}");
        return request;
    }

    // 随机生成的6位的随机数
    public static String RandomCode() {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 1; i <= 6; i++) {
            int nextInt = random.nextInt(10);
            stringBuilder.append(nextInt);
        }
        return stringBuilder.toString();
    }
}
